package ar.org.cfired.creditos.eventlistener;
import java.util.List;

import org.apache.log4j.Logger;
import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.TaskQuery;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.task.Task;

import ar.org.cfired.creditos.gui.action.*;
import ar.org.cfired.creditos.parametros.GuardarParametrosException;

public class TaskStartRechazadosListener implements EventListener {

	private static final long serialVersionUID = 1L;
	private Logger objetoLogger = Logger.getLogger(this.getClass().getName());

	//@Override
	public void notify(EventListenerExecution execution) throws Exception {		
		objetoLogger.debug("Enviando");
		RechazarAction accion = (RechazarAction)ActionFactory.getAction("rechazar");
		String exId = execution.getId();
				
		String exId2 = exId.substring(0,exId.indexOf("."));
		String resto = exId.substring(exId.indexOf(".") + 1);
		objetoLogger.debug("Resto: " + resto);
		if (resto.contains("."))
			exId2 += "." + resto.substring(0,resto.indexOf("."));
		else
			exId2 += "." + resto;
		objetoLogger.debug("exID: " + exId2);
		ProcessEngine proc = new Configuration().buildProcessEngine();
		objetoLogger.debug("Process Engine: " + exId2);
		TaskQuery tq = proc.getTaskService().createTaskQuery();
		objetoLogger.debug("Task Query: " + exId2);
		tq.processInstanceId(exId2);
		List<Task> tareas = tq.list();
		objetoLogger.debug("List: " + exId2);
		for (int i=0; i<tareas.size(); i++) {
			Task t = tareas.get(i);
			//objetoLogger.debug(t.getId()); 
			try {
				accion.execute(t.getId(), t.getAssignee(), t.getExecutionId());
			}
			catch (GuardarParametrosException exc) {				
			}
		}
		
	}
	
}